﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Help;
using Model;
namespace BLLA
{
    public partial class Order_MerSendAdd_T : BaseBLL<Model.Order_MerSendAdd_T>, IBLL.IOrder_MerSendAdd_TBLL
    {
        ObjectSerialize os = new ObjectSerialize();
        DataResult result = new DataResult();
        public string GetAllMerSendAdd(int pageIndex, int pageSize, string YZM, int RETID)
        {
            try
            {
                #region 匹配验证码
                if (YZM != Help.ALL_DataV.YZM)
                {
                    result.BS = "-88";
                    result.Msg = "验证码错误！";
                    return os.StringOfObject(result, RETID);
                }
                #endregion
                #region 执行操作
                List<Model.Order_MerSendAdd_T> ls = base.GetListBy(u =>u.AddStat==1);
                if (ls != null & ls.Count > 0)
                {
                    result.BS = "1";
                    result.Msg = "查询成功";
                    result.total = ls.Count;
                    result.rows = ls.Skip((pageIndex - 1) * pageSize).Take(pageSize).ToList();
                }
                else
                {
                    result.BS = "0";
                    result.Msg = "查询不到数据";
                }
                #endregion
                return os.StringOfObject(result, RETID);
            }
            catch (Exception ex)
            {
                WriteLog.WriteW(" 查询所有商户的评价管理信息失败：" + ex.Message);
                result.BS = "-99";
                result.Msg = "系统错误！";
                return os.StringOfObject(result, RETID);
            }
        }
        public string SetOrder_MerSendAdd(string json, int tag, string YZM, int RETID)
        { 
            #region 匹配验证码
            if (YZM != Help.ALL_DataV.YZM)
            {
                result.BS = "-88";
                result.Msg = "验证码错误！";
                return os.StringOfObject(result, RETID);
            }
            #endregion
            #region 执行操作
            try
            {
                Model.Order_MerSendAdd_T info = ParseClass<Model.Order_MerSendAdd_T>.StrDeserialize(json, RETID);
                string T = "";
                int ret = -1;
                // 根据操作标签Tag判断是什么操作
                switch (tag)
                {
                    case 1:
                        T = "新增"; 
                        info.AddId = int.Parse( GetSysKey.GetKey("OrderSendAddKey"));
                        ret = base.Add(info);
                        break;
                    case 2:
                        T = "修改";
                        Model.Order_MerSendAdd_T tmp = base.GetListBy(u => u.AddId == info.AddId).First();
                        Help.FuZhi<Model.Order_MerSendAdd_T>.FZ(ref tmp, info);
                        ret = base.Modify(tmp);
                        break;
                    case 3:
                        T = "删除";
                        List<Model.Order_MerSendAdd_T> list_tmp = base.GetListBy(u => u.AddId == info.AddId);
                        
                        if (list_tmp != null && list_tmp.Count > 0)
                        {
                            tmp = list_tmp.First();
                            tmp.AddStat = 0;
                            ret = base.Modify(tmp);
                            List<Model.Order_MerSend_T> MsList = base.DBSession.IOrder_MerSend_TDAL.GetListBy(u => u.MerSendAddId == info.AddId);
                            for (int i = 0; i < MsList.Count; i++)
                            {
                                MsList[i].Stat = 0;
                                base.DBSession.IOrder_MerSend_TDAL.Modify(MsList[i]);
                            }
                        } 
                        break;
                }

                if (ret > 0)
                {
                    
                    result.BS = "1";
                    result.Msg = "更新成功";
                }
                else
                {
                    result.BS = "0";
                    result.Msg = "更新失败";
                }
            }
            catch (Exception ex)
            {
                WriteLog.WriteW("更新商户信息失败：" + ex.Message);
                result.BS = "-1";
                result.Msg = "更新失败";
            }
            #endregion
            return os.StringOfObject(result, RETID);
        }

        public string GetMerAddFromId(int pageIndex, int pageSize,int AddId, string YZM, int RETID)
        {
            try
            {
                #region 匹配验证码
                if (YZM != Help.ALL_DataV.YZM)
                {
                    result.BS = "-88";
                    result.Msg = "验证码错误！";
                    return os.StringOfObject(result, RETID);
                }
                #endregion
                #region 执行操作
                List<string> order_Mer_Ts = base.DBSession.IOrder_Mer_TDAL.GetListBy(u => u.MerState == 1).Select(u => u.MerID).ToList();
                List<Model.View_Order_MerSend> ls = base.DBSession.IView_Order_MerSendDAL.GetListBy(u => order_Mer_Ts.Contains(u.MerId)&u.MerSendAddId==AddId&u.Stat==1);

                if (ls != null & ls.Count > 0)
                {
                    result.BS = "1";
                    result.Msg = "查询成功";
                    result.total = ls.Count;
                    result.rows = ls.Skip((pageIndex - 1) * pageSize).Take(pageSize).ToList();
                }
                else
                {
                    result.BS = "0";
                    result.Msg = "查询不到数据";
                }
                #endregion
                return os.StringOfObject(result, RETID);
            }
            catch (Exception ex)
            {
                WriteLog.WriteW(" 查询所有商户的评价管理信息失败：" + ex.Message);
                result.BS = "-99";
                result.Msg = "系统错误！";
                return os.StringOfObject(result, RETID);
            }
        }

        public string GetAllAddByUser(string YZM, int RETID)
        {
            try
            {
                #region 匹配验证码
                if (YZM != Help.ALL_DataV.YZM)
                {
                    result.BS = "-88";
                    result.Msg = "验证码错误！";
                    return os.StringOfObject(result, RETID);
                }
                #endregion
                #region 执行操作
                List<Model.View_Order_MerSend> view_Order_MerSends = new List<Model.View_Order_MerSend>();

                List<Model.Order_MerSendAdd_T> ls = base.GetListBy(u => u.AddStat == 1);
                foreach (var item in ls)
                {
                    view_Order_MerSends.Add(new Model.View_Order_MerSend()
                    {
                        MerSendAddId = item.AddId,
                        AddName = item.AddName
                    });
                }
                if (ls != null & ls.Count > 0)
                {
                    result.BS = "1";
                    result.Msg = "查询成功";
                    result.total = ls.Count;
                    result.rows = view_Order_MerSends;
                }
                else
                {
                    result.BS = "0";
                    result.Msg = "查询不到数据";
                }
                #endregion
                return os.StringOfObject(result, RETID);
            }
            catch (Exception ex)
            {
                WriteLog.WriteW(" 查询所有商户的评价管理信息失败：" + ex.Message);
                result.BS = "-99";
                result.Msg = "系统错误！";
                return os.StringOfObject(result, RETID);
            }
        }
    }
}
